<?php
/**
 * Created by PhpStorm.
 * User: fengzi
 * Date: 2019/5/29
 * Time: 17:19
 */

namespace app\logic;
use app\business\model\City;
use app\business\model\Config;
use app\business\model\BusSeat;
use app\business\model\BusNum;
use app\business\model\BusLimit;
use app\business\model\BusBrand;
use app\common\model\Order;
use app\common\model\DeliveryOrder;
use TCPDF;
class commomLogic
{
    function contract_pdf($data){
        $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
        $pdf->setPrintHeader(false);    //页面头部横线取消
        $pdf->setPrintFooter(false); //页面底部更显取消
        $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);//自动分页
        $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);//设置页面margin
        $pdf->SetFont('stsongstdlight', '', 12);//设置字体,注意在循环里面一定要把new都一起放在循环里面，不然会报错，没有设置字体，因为这个需要上下文来读取配置
        $pdf->AddPage();//增加一个页面
        $pdf->setCellPaddings(0, 0, 0, 0);//设置一个单元格的padding
        //追加用户亲笔签名到文件底部
        $contract_content = Config::where('id','eq',9)->value('value');
        $order = Order::with('signature')->where('id','eq',$data['order_id'])->field('signature_id')->find();
        $order['thumb'] && $contract_content .= '<div><span>旅行社：</span><img height="50" src="'.$order['thumb'].'"></div>';

        $business_order = DeliveryOrder::with('signature')->where('id','eq',$data['delivery_id'])->field('signature_id')->find();
        $business_order['thumb'] && $contract_content .= '<div><span>车企：</span><img height="50" src="'.$business_order['thumb'].'"></div>';
        //tcpdf支持远程图片，所以不用麻烦
        $pdf->writeHTML($contract_content, $ln = true, $fill = false, $reseth = false, $cell = false, $align = '1');//这个如果里面有远程图片，不能直接获取，需要在代码本地才行，把
        $pdf->Output('contract.pdf', $data['type']);
        die;
    }
    //获取小城序登录logo
    function getLogoUrl(){
        return Config::where('id','eq',4)->value('value');
    }
    //获取城市列表
    function getCityList(){
        $first_list = db('city')->where('pid','eq','0')->field('id,name')->select();
        $first_list[count($first_list)] = array('id'=>0,'name'=>'热门');
        $first_list = array_reverse($first_list);
        foreach($first_list as $k=>$v){
            if($v['id']>0){
                $first_list[$k]['child'] = City::where('pid','eq',$v['id'])->field('id,name')->select();
            }else{
                $first_list[$k]['child'] = City::where('is_hot','eq',1)->where('pid','gt',0)->field('id,name')->select();
            }
        }
        return $first_list;
    }
    //获取客服电话
    function getContactMobile(){
        return Config::where('id','eq',8)->value('value');
    }
}